<template>
	<view class="container">
		<u--textarea v-model="content" height="140" placeholder="有问必答，百万同行为你解答汽车相关问题！" border="none"></u--textarea>
		<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="fileList" multiple
			:maxCount="maxCount" :width="160" :height="160" :accept="accept"></u-upload>
		<view class="footer">
			<u-button type="primary" color="#06A972" text="发布" @click="shareUploadVideo"></u-button>
		</view>

	</view>
</template>


<script>
	import {
		HTTP_REQUEST_URL_NEW
	} from '@/config/app';
	export default {
		data() {
			return {
				// iconType: 'cancel',
				// videoList: [],
				// imgArr: "",
				// pingjiaSrc: "@/static/images/up1.png",
				// videoShow: true,
				content: '',
				maxCount: 1,
				fileList: [],
				list: [],
				fileListLen: 0,
				accept: 'video',
			}

		},
		
		onLoad(option) {
			let that = this, title, year;
			that.carId = option.carId;
			title = option.title;
			year = option.year;
			console.log(option);
			if (!that.carId) {
				return that.$util.Tips({
					title: '车型不存在'
				}, {
					tab: 3
				});
			}
			uni.setNavigationBarTitle({
				title: title + ' ' + year
			})
		},
		
		methods: {
			// // 上传视频
			// chooseVideo() {
			// 	let that = this;

			// 	uni.chooseVideo({
			// 		count: that.maxCount,
			// 		maxDuration: 60, // 拍摄视频最长拍摄时间，单位秒。最长支持 60 秒。
			// 		sourceType: ['album', 'camera'], // album 从相册选视频，camera 使用相机拍摄，默认为：['album', 'camera']
			// 		success(res) {
			// 			console.log(res)
			// 			that.videoList = that.videoList.concat(res.tempFilePath);
			// 			if (that.videoList.length == that.maxCount) {
			// 				that.videoShow = false;
			// 			}
			// 		}
			// 	})
			// },

			// delVideo(index) {
			// 	uni.showModal({
			// 		title: "提示",
			// 		content: "是否要删除此视频",
			// 		success: (res) => {
			// 			if (res.confirm) {
			// 				this.videoList.splice(index, 1)
			// 			}
			// 		}
			// 	})
			// },

			// shareUploadVideo() {
			// 	let that = this;
			// 	if (!that.content) return that.$util.Tips({
			// 		title: '请输入内容'
			// 	})
			// 	uni.showLoading({
			// 		mask: true,
			// 		title: '上传中...'
			// 	})
			// 	uni.uploadFile({
			// 		// 需要上传的地址
			// 		url: HTTP_REQUEST_URL_NEW + '/api/car/shareUploadVideo',
			// 		// filePath  需要上传的文件
			// 		filePath: that.videoList[0],
			// 		name: 'shareVideo',
			// 		formData: {
			// 			carId: 11277,
			// 			userInfoId: that.$store.state.app.uid,
			// 			shareContent: that.content,
			// 		},
			// 		success: (res) => {
			// 			console.log(res);
			// 			uni.hideLoading();
			// 			that.$util.Tips({
			// 				title: '发布成功',
			// 			}, {
			// 				tab: 3
			// 			})
			// 		},
			// 		fail: (err) => {
			// 			that.$util.Tips({
			// 				title: '上传失败',
			// 			});
			// 			uni.hideLoading();
			// 		}
			// 	});
			// },



			// 删除图片
			deletePic(event) {
				this[`${event.name}`].splice(event.index, 1)
			},
			// // 新增图片
			afterRead(event) {
				let that = this;
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				that.list = [].concat(event.file)
				that.fileListLen = that[`${event.name}`].length
				that.list.map((item) => {
					that[`${event.name}`].push({
						...item,
						status: 'success',
						message: ''
					})
				})
				console.log(that.fileList);
			},

			async shareUploadVideo() {
				let that = this;

				if (!that.content) return that.$util.Tips({
					title: '请输入内容'
				})

				for (let i = 0; i < that.list.length; i++) {
					const result = await that.uploadFilePromise(that.list[i].url)
					if (result.resultCode) {
						that.$util.Tips({
							title: '发布成功',
						}, {
							tab: 3
						})
					}
				}
			},

			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: HTTP_REQUEST_URL_NEW + '/api/car/shareUploadVideo', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'shareVideo',
						formData: {
							carId: that.carId,
							userInfoId: this.$store.state.app.uid,
							shareContent: this.content,
						},
						success: (res) => {
							setTimeout(() => {
								resolve(JSON.parse(res.data))
							}, 1000)
						}
					});
				})
			},


		}
	}
</script>

<style>
	.container {
		padding: 40rpx;
	}

	.container .uni-textarea {
		height: 225rpx;
	}
	
	.u-upload__button .u-icon__icon {
		font-size: 28px !important;
	}

	.u-upload__deletable__icon .u-icon__icon {
		font-size: 20rpx !important;
		line-height: 20rpx !important;
	}

	.u-upload__success__icon .u-icon__icon {
		font-size: 24rpx !important;
		line-height: 24rpx !important;
	}
</style>